/*****************************************************************************
 * Copyright (c) 2010 -, Anders All rights reserved
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  Contact: Anders Uddenberg (anders.uddenberg@gmail.com)
 * 
 *
 ****************************************************************************/
package org.easypr.organizer.client.view;

import java.util.ArrayList;
import java.util.Date;

import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import org.easypr.organizer.client.presenter.ContactPresenter;
import org.easypr.organizer.shared.MyContact;
import org.easypr.organizer.shared.MyMeeting;

// TODO: Auto-generated Javadoc
/**
 * The Class MenuView.
 */
public class ContactView extends Composite implements ContactPresenter.Display {

	/** The container. */
	FlowPanel container;
	
	/** The details wrapper. */
	FlowPanel detailsWrapper;
	
	/** The contact. */
	MyContact contact;
	
	/** The previous link. */
	Anchor previousLink;
	
	/** The next link. */
	Anchor nextLink;
	
	/** The edit link. */
	Anchor editLink;
	
	/** The delete link. */
	Anchor deleteLink;
	
	/** The save edit link. */
	Anchor saveEditLink;
	
	/** The contact email. */
	TextBox contactEmail;
	
	/** The contact name. */
	TextBox contactName;
	
	/** The contact workplace. */
	TextBox contactWorkplace;
	
	/** The contact workphone. */
	TextBox contactWorkphone;
	
	/** The contact cellphone. */
	TextBox contactCellphone;
	
	/** The boxes. */
	ArrayList<CheckBox> boxes;
	
	/** The delete meetings link. */
	Anchor deleteMeetingsLink;
	// When paging - this indicates which meetings to get
	/** The before time. */
	int beforeTime = 0;
	// When paging - we need to save the last before time so we can go back
	/** The last before time. */
	int lastBeforeTime = 0;

	/**
	 * Instantiates a new menu view.
	 */
	public ContactView() {
		container = new FlowPanel();
		initWidget(container);
		previousLink = new Anchor("Previous");
		nextLink = new Anchor("Next");
		editLink = new Anchor("Edit");
		deleteLink = new Anchor("Delete");
		saveEditLink = new Anchor("Save");
		contactEmail = new TextBox();
		contactName = new TextBox();
		contactWorkplace = new TextBox();
		contactWorkphone = new TextBox();
		contactCellphone = new TextBox();
		detailsWrapper = new FlowPanel();
		boxes = new ArrayList<CheckBox>();
		deleteMeetingsLink = new Anchor("Delete selcted");
		
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#showUserEdit()
	 */
	public void showUserEdit() {
		this.detailsWrapper.clear();
		
		this.contactEmail.setText(this.contact.getEmail());
		this.contactName.setText(this.contact.getName());
		this.contactWorkplace.setText(this.contact.getWorkplace());
		this.contactWorkphone.setText(this.contact.getWorkphone());
		this.contactCellphone.setText(this.contact.getCellphone());
		
		this.detailsWrapper.add(getUserEdit());
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#getCheckBoxes()
	 */
	public ArrayList<CheckBox> getCheckBoxes() {
		return this.boxes;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#getDeleteMeetingsLink()
	 */
	public Anchor getDeleteMeetingsLink() {
		return this.deleteMeetingsLink;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#showUserDetails()
	 */
	public void showUserDetails() {
		this.detailsWrapper.clear();
		this.detailsWrapper.add(getUserDetails());
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#getContactEmail()
	 */
	public TextBox getContactEmail() {
		return contactEmail;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#getContactName()
	 */
	public TextBox getContactName() {
		return contactName;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#getContactWorkplace()
	 */
	public TextBox getContactWorkplace() {
		return contactWorkplace;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#getContactWorkphone()
	 */
	public TextBox getContactWorkphone() {
		return contactWorkphone;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#getContactCellphone()
	 */
	public TextBox getContactCellphone() {
		return contactCellphone;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#getSaveEditLink()
	 */
	public Anchor getSaveEditLink() {
		return this.saveEditLink;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#getBeforeTime()
	 */
	public int getBeforeTime() {
		return this.beforeTime;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#getLastBeforeTime()
	 */
	public int getLastBeforeTime() {
		return this.lastBeforeTime;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#getPreviousLink()
	 */
	public Anchor getPreviousLink() {
		return this.previousLink;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#getNextLink()
	 */
	public Anchor getNextLink() {
		return this.nextLink;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#getEditLink()
	 */
	public Anchor getEditLink() {
		return this.editLink;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#getDeleteLink()
	 */
	public Anchor getDeleteLink() {
		return this.deleteLink;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#setContact(org.easypr.organizer.shared.MyContact)
	 */
	public void setContact(MyContact contact) {
		this.contact = contact;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#getContact()
	 */
	public MyContact getContact() {
		return this.contact;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#updateNavigation(java.lang.Integer, java.lang.Boolean)
	 */
	public void updateNavigation(Integer previous, Boolean next) {
		
		if(previous > 0 || next) {
			
			FlexTable navigation = new FlexTable();
			navigation.setWidth("99%");
			if(previous > 0) {
				this.lastBeforeTime = (previous == 1 ? 0 : previous);
				navigation.setWidget(0, 0, previousLink);
			}
			if(next) {
				navigation.setWidget(0, 1, nextLink);
				navigation.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_RIGHT);
			}
			
			container.add(navigation);
			
		}
		
	}
	
	/**
	 * Gets the user details.
	 *
	 * @return the user details
	 */
	public FlowPanel getUserDetails() {
		
		FlowPanel details = new FlowPanel();
		details.setStyleName("contact-details");
		
		details.add(new Label("Contact details"));
		FlexTable detailsTable = new FlexTable();
		detailsTable.setWidget(0, 0, new Label("Email: " + this.contact.getEmail()));
		detailsTable.getFlexCellFormatter().setWidth(0, 0, "250px;");
		detailsTable.setWidget(0, 1, new Label("Workplace: " + this.contact.getWorkplace()));
		if(this.contact.getWorkphone().length() > 0) {
			detailsTable.setWidget(1, 0, new Label("Workphone: " + this.contact.getWorkphone()));
		}
		if(this.contact.getCellphone().length() > 0) {
			detailsTable.setWidget(1, 1, new Label("Cellphone: " + this.contact.getCellphone()));
		}
		detailsTable.setWidget(2, 0, editLink);
		detailsTable.setWidget(2, 1, deleteLink);
		
		details.add(detailsTable);
		
		return details;
	}
	
	/**
	 * Gets the user edit.
	 *
	 * @return the user edit
	 */
	public FlowPanel getUserEdit() {
		
		FlowPanel edit = new FlowPanel();
		edit.setStyleName("contact-details");
		
		edit.add(new Label("Contact details"));
		FlexTable detailsTable = new FlexTable();
		
		detailsTable.setWidget(0, 0, new Label("Name"));
		detailsTable.setWidget(1, 0, contactName);
		detailsTable.getFlexCellFormatter().setWidth(0, 0, "300px;");
		
		detailsTable.setWidget(0, 1, new Label("Email"));
		detailsTable.setWidget(1, 1, contactEmail);
		
		detailsTable.setWidget(2, 0, new Label("Workphone"));
		detailsTable.setWidget(3, 0, contactWorkphone);
		
		detailsTable.setWidget(2, 1, new Label("Cellphone"));
		detailsTable.setWidget(3, 1, contactCellphone);
		
		detailsTable.setWidget(4, 0, new Label("Workplace"));
		detailsTable.setWidget(5, 0, contactWorkplace);
		
		detailsTable.setWidget(6, 0, saveEditLink);
		
		edit.add(detailsTable);
		
		return edit;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#setData(java.util.ArrayList)
	 */
	public void setData(ArrayList<MyMeeting> meetings) {

		container.clear();
		detailsWrapper.clear();
		FlexTable bodycontent = new FlexTable();
		
		HorizontalPanel header = new HorizontalPanel();
		header.setStyleName("content-header");
		
		FlowPanel body = new FlowPanel();
		body.setStyleName("content-body");

		Label lbl = new Label("Meeting notes for " + this.contact.getName());
		lbl.setStyleName("header-label");
		
		header.add(lbl);
		container.add(header);
		detailsWrapper.setStyleName("contact-details");
		detailsWrapper.add(getUserDetails());
		container.add(detailsWrapper);
		
		

		if(meetings.size() > 0) {
			for(int i = 0; i<meetings.size(); i++) {
				bodycontent.setWidget(i, 0, this.getMeetingWidget(meetings.get(i)));
				if(i == (meetings.size()-1)) {
					this.beforeTime = meetings.get(i).getDate();
				}
				
			}
			bodycontent.setWidget(meetings.size(), 0, deleteMeetingsLink);
		} else {
			bodycontent.setWidget(0, 0, new Label("No meetings found!"));
		}
		
		
		
		container.add(bodycontent);
	}
	
	/**
	 * Gets the meeting widget.
	 *
	 * @param meeting the meeting
	 * @return the meeting widget
	 */
	@SuppressWarnings("deprecation")
	public FlowPanel getMeetingWidget(MyMeeting meeting) {
		
		/*
		 * Building the widget 
		 */
		
		FlowPanel meetingWidget = new FlowPanel();
		meetingWidget.addStyleName("widget-large");

		// Header
		HorizontalPanel meetingHeader = new HorizontalPanel();
		meetingHeader.setStyleName("widget-header");
			
		Date date = new Date((long)(meeting.getDate())*1000);
		date.setMonth(date.getMonth()+1);
		date.setYear(date.getYear()+1900);
		
		Label meetingLabel = new Label("Meeting @ " + Integer.toString(date.getDate()) + "/" + Integer.toString(date.getMonth()) + "/" + Integer.toString(date.getYear()));
		meetingLabel.setStyleName("widget-heading");
			
		meetingHeader.add(meetingLabel);

		CheckBox box = new CheckBox("select");
		box.getElement().setId(Long.toString(meeting.getId()));
		this.boxes.add(box);
		meetingHeader.add(box);
		

		// Body
		FlexTable meetingBody = new FlexTable();
		meetingBody.setStyleName("widget-body");
		meetingBody.setCellSpacing(0);
		
		meetingBody.setWidth("99%");
		

		FlowPanel meetingNotes = new FlowPanel();
		Label text = new Label(meeting.getNotes());
		meetingNotes.add(text);
		meetingNotes.setWidth("100%");
		//meetingBody.getFlexCellFormatter().setColSpan(2, 0, 3);
		meetingBody.setWidget(0, 0, meetingNotes);
		
		// Assemble the Widget
		meetingWidget.add(meetingHeader);
		meetingWidget.add(meetingBody);
		
		return meetingWidget;
	}

	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ContactPresenter.Display#asWidget()
	 */
	@Override
	public Widget asWidget() {
		return this;
	}
	
}
